package com.jcxy.service.impl;

import com.jcxy.VO.UserVo;
import com.jcxy.mapper.RoleMapper;
import com.jcxy.mapper.UserMapper;
import com.jcxy.pojo.Role;
import com.jcxy.pojo.User;
import com.jcxy.service.UserService;
import com.jcxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User login(Map paramDto) {

        //执行sql语句，查询user
        User user = userMapper.login(paramDto);
        if(user != null){
            //登录成功，则查询用户权限
            Role role = roleMapper.queryRoleJoinMenuById(user.getRole_id());
            user.setRole(role);
        }
        return user;
    }

    @Override
    public PageBean queryListByPage(Map dto, Integer page, Integer limit) {

        Integer count = userMapper.queryUserCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<UserVo> userList = userMapper.queryUser(dto);

        pageBean.setResponseData(count,userList);
        return pageBean;
    }

    @Override
    @Transactional
    public int insertUser(Map dto) throws Exception {

        return userMapper.insertUser(dto);
    }

    @Override
    @Transactional
    public int updateUser(Map dto) throws Exception {


        int count = userMapper.updateUser(dto);
        if(count > 1){
            throw new Exception("更新用户异常！");
        }
        return count;
    }

    @Override
    @Transactional
    public int deleteUserById(Integer user_id) throws Exception {

        int count = userMapper.logicDeleteUser(user_id);
        if(count > 1){
            throw new Exception("删除用户异常！");
        }
        return count;
    }
}
